function Observer(data) {
  this.data = data;
  this.walk(data);
}

Observer.prototype = {
  walk(data) {
    Object.keys(data).forEach(key => {
      this.defineReactive(data, key, data[key]);
    });
  },
  defineReactive(data, key, value) {
    const dep = new Dep();
    observer(value);
    Object.defineProperty(data, key, {
      enumerable: true,
      configurable: true,
      get() {
        if (Dep.target) {
          dep.addDep(Dep.target);
        }
        return value;
      },
      set(newVal) {
        if (value === newVal) return;
        console.log("set 更新=>" + key);
        value = newVal;
        observer(newVal);
        dep.notify();
      }
    });
  }
};

function observer(value) {
  if (!value || typeof value !== "object") return;
  new Observer(value);
}

function Dep() {
  this.subs = [];
}

Dep.prototype = {
  addDep(sub) {
    this.subs.push(sub);
  },
  notify() {
    this.subs.forEach(sub => {
      sub.update();
    });
  }
};
Dep.target = null;
// const data = {
//   page: 1,
//   limit: 2,
//   // arr: [6, 7, 8],
//   info: {
//     title: 'good luck'
//   }
// }
// observer(data)
